using OracleInternal.BulkCopy;
using OracleInternal.Common;

namespace OracleInternal.TTC
{
	internal class TTCDirectPathStreamMarshaller
	{
		private MarshallingEngine m_Engine;

		private IBulkCopyDataSource m_dataSource;

		private ColumnDescribeInfo[] m_columnsMetaData;

		private DirectPathRow m_currentRow;

		private DirectPathRowPiece m_currentPiece;

		private BulkCopyBindingRow m_currentBindingRow;

		internal int m_numOfRowsToBeProcessed;

		internal bool m_bAllFastColumns;

		internal bool m_bIsComplete;

		internal int m_numOfRowsProcessed;

		internal TTCDirectPathStreamMarshaller()
		{
		}

		internal void Init(MarshallingEngine mEngine, IBulkCopyDataSource dataSource, ColumnDescribeInfo[] columnsMetaData, int batchSize)
		{
			m_Engine = mEngine;
			m_dataSource = dataSource;
			m_columnsMetaData = columnsMetaData;
			m_bAllFastColumns = IsAllFastType(columnsMetaData);
			m_currentRow = null;
			m_currentPiece = null;
			m_currentBindingRow = null;
			m_numOfRowsProcessed = 0;
			m_bIsComplete = false;
			m_bAllFastColumns = false;
			m_numOfRowsProcessed = 0;
		}

		internal static bool IsFastType(int typeCode)
		{
			if (typeCode != 1 && typeCode != 96 && typeCode != 185 && typeCode != 188 && typeCode != 186 && typeCode != 180 && typeCode != 187 && typeCode != 231 && typeCode != 232 && typeCode != 181 && typeCode != 188 && typeCode != 182 && typeCode != 189 && typeCode != 183 && typeCode != 190 && typeCode != 2 && typeCode != 12 && typeCode != 23 && typeCode != 100 && typeCode != 101 && typeCode != 6)
			{
				return typeCode == 252;
			}
			return true;
		}

		internal static bool IsAllFastType(ColumnDescribeInfo[] columnsMetaData)
		{
			bool flag = true;
			int num = 0;
			while (flag && num < columnsMetaData.Length)
			{
				flag = IsFastType(columnsMetaData[num].m_dataType);
				num++;
			}
			return flag;
		}

		internal static int RowPieceHeaderLen(short flags)
		{
			if (flags == 60)
			{
				return 4;
			}
			return 2;
		}

		internal static int MarshalRowPieceHeader(MarshallingEngine mEngine, DirectPathRowPiece rowPiece)
		{
			short flags = rowPiece.Flags;
			mEngine.MarshalUB1(flags);
			if (flags == 60)
			{
				mEngine.MarshalNativeUB2(rowPiece.RowPieceLen);
			}
			mEngine.MarshalUB1((short)rowPiece.m_colCount);
			if (flags == 60)
			{
				return 4;
			}
			return 2;
		}

		private static void MarshalColLength(MarshallingEngine mEngine, int length)
		{
			if (((length <= 250) ? 1 : 3) == 1)
			{
				mEngine.MarshalUB1((byte)length);
				return;
			}
			mEngine.MarshalUB1(254);
			mEngine.MarshalNativeUB2((short)length);
		}

		internal static void MarshallRowPiece(MarshallingEngine mEngine, BulkCopyBindingRow row, DirectPathRowPiece rowPiece)
		{
			MarshalRowPieceHeader(mEngine, rowPiece);
			for (int i = rowPiece.m_firstColIndex; i <= rowPiece.m_endColIndex; i++)
			{
				if (row.m_col_nullIndicators[i])
				{
					mEngine.MarshalUB1(255);
					continue;
				}
				if (row.m_bindValuesLen[i] == 0)
				{
					MarshalColLength(mEngine, 0);
					continue;
				}
				int beginColDataOffset = rowPiece.m_beginColDataOffset;
				int endColDataOffset = rowPiece.m_endColDataOffset;
				int length;
				if (rowPiece.m_bContinueFromPrevPiece || rowPiece.m_bContinueToNextPiece)
				{
					if (rowPiece.m_bContinueFromPrevPiece)
					{
						beginColDataOffset = rowPiece.m_beginColDataOffset;
					}
					if (rowPiece.m_bContinueToNextPiece)
					{
						endColDataOffset = rowPiece.m_endColDataOffset;
					}
					length = endColDataOffset - beginColDataOffset + 1;
				}
				else
				{
					length = row.m_bindValuesLen[i];
				}
				MarshalColLength(mEngine, length);
				mEngine.MarshalBytes(row.m_columnsBindValues[i], beginColDataOffset, length);
			}
		}
	}
}
